package com.auto.nio;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannelDemo01 {
    public static void main(String[] args) throws Exception{
        String str="hello,网络编程";
        FileOutputStream fos=new FileOutputStream("D:\\file01.txt");
        //根据输出流获取通道，实际类型为FileChannelImpl
        FileChannel fileChannel = fos.getChannel();
        //创建ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        //将字符串放入缓冲区
        byteBuffer.put(str.getBytes());

        //翻转
        byteBuffer.flip();

        //将缓冲区数据写入通道中
        fileChannel.write(byteBuffer);

        fos.close();
    }
}
